import $http from "./request.js";
// 网络监听
const NetWork = {
	// 网络状态
	isConnect:false,
	// 监听网络状态
	On(){
		// 获取当前网络状态
		uni.getNetworkType({
			success: (res) => {
				if(res.networkType!=='none'){ this.isConnect=true; return;}
				uni.showToast({
					icon:"none",
					title: '请先连接网络',
				});
			}
		})
		// 监听网络状态变化
		uni.onNetworkStatusChange((res)=>{
			this.isConnect = res.isConnected;
			if(!res.isConnected){
				uni.showToast({
					icon:"none",
					title: '您目前处于断网状态',
				});
			} 
		})
	}
}
// app更新
const Update = function(isClickCheckFlag,currPage){
	// #ifdef APP-PLUS
	plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
		uni.setStorageSync('currVersion',widgetInfo.version);
		$http.get('/common/getAppVersion',{useArea:'user'}).then((res) => {
			let [err,result] = res;
			if (!$http.errorCheck(err,result)) return;
			if(res[1].data.code == 200){
				if(res[1].data.datas.length > 0){
					let tmpRes = res[1].data.datas[0];
					if(tmpRes.version != widgetInfo.version && tmpRes.versionCode > widgetInfo.versionCode){
						let { upLog,mustUpTag,suffix } = tmpRes;
						if(currPage == 'indexPage'){
							setTimeout(()=>{
								jumpUpdatePage(upLog,mustUpTag,suffix);
							},1000)
						}else{
							jumpUpdatePage(upLog,mustUpTag,suffix);
						}
					}else{
						if(isClickCheckFlag){
							uni.showToast({title: '已是最新版本',icon:'none'});
						}
					}
				}
			}else if(res[1].data.code == 1000){}
			else if(res[1].data.code == 50001){}
		});
	});  
	// #endif  
}
function jumpUpdatePage(upLog,mustUpTag,suffix){
	uni.navigateTo({
		url:'/pages/update/update?upLog=' + upLog + '&mustUpTag=' + mustUpTag + '&suffix=' + suffix
	})
}
export default {
	NetWork,
	Update
}